//
//  TZWStatusCell.swift
//  Sinaweibotzw
//
//  Created by 檀志文 on 16/4/15.
//  Copyright © 2016年 ios檀志文. All rights reserved.
//

import UIKit

import SnapKit

class TZWStatusCell: UITableViewCell {

//    记录之前bottomView 的top 约束
    var bottomViewTopCons: Constraint?
    
//   微博模型
    
    var status: TZWStatus? {
        didSet {
            // 当控制器给cell设置模型后,来做操作
            // 将微博模型传递给originalView
            originalView.status = status
            
//      先卸载掉之前的bottomView的顶部约束
            bottomViewTopCons?.uninstall()
//       如果有被转发的微博,将被转发的微博设置给被转发的view
            if let retweenStatus = status?.retweeted_status{
                retweenView.hidden = false
                retweenView.retweetStatus = retweenStatus
                
//       有被转发微博的时候bottomView的顶部参照被转发view的底部
            bottomView.snp_makeConstraints(closure: { (make) -> Void in
                bottomViewTopCons = make.top.equalTo(retweenView.snp_bottom).constraint
            })
            }else{
//       如果没有被转发的微博,将被转发的view隐藏
            retweenView.hidden = true
//      将bottomView的顶部约束参照原创view的底部
                bottomView.snp_makeConstraints(closure: { (make) -> Void in
                    bottomViewTopCons = make.top.equalTo(originalView.snp_bottom).constraint
                })
           
            }
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style:style, reuseIdentifier: reuseIdentifier)
//       设置颜色
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1)
        prepareUI()
    }
    
    private func prepareUI() {
        
        
        
//        1 添加子控件   
        contentView.addSubview(originalView)
        contentView.addSubview(bottomView)
        contentView.addSubview(retweenView)
        
        
//        2 添加约束
        originalView.snp_makeConstraints { (make) -> Void in
            make.leading.right.equalTo(contentView)
            // 顶部有10得间距
            make.top.equalTo(contentView).offset(10)
            
        }
        
//        3 被转发的view
        retweenView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(originalView.snp_bottom)
            make.leading.trailing.equalTo(contentView)
        }
        
        bottomView.snp_makeConstraints { (make) -> Void in
            make.leading.trailing.equalTo(contentView)
//            make.top.equalTo(originalView.snp_bottom)
            make.height.equalTo(37)
            
//           bottomview 的顶部参照被转发view的底部
            bottomViewTopCons = make.top.equalTo(retweenView.snp_bottom).constraint
        }
        contentView.snp_makeConstraints { (make) -> Void in
            make.leading.top.trailing.equalTo(self)
            
            make.bottom.equalTo(bottomView)
        }

        
    }
    
//   MARK: 懒加载
    private lazy var originalView: TZWStatusOriginalView = TZWStatusOriginalView()
    
//    底部的view
    private lazy var bottomView: TZWBottomView = TZWBottomView()
    
//   被转发的微博
    private lazy var retweenView: TZWStatusRetweetView = TZWStatusRetweetView()
    
    
}
